﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrainingWorkshop.Model;
using System.Data;
using System.Data.SqlClient;

namespace TrainingWorkshop.DAL
{
	class DepartMentOp:IDepartmentRepo
	{
        private SqlConnection sqlconn;

        private DepartmentRepository DepartmentInstance;

        public DepartmentOp(IFactory<SqlConnection> TConn,DepartmentRepository Department)
        {
           sqlconn = TConn.GetConnectObject();
           DepartmentInstance.DepartmentId=Department.DepartmentId;
           DepartmentInstance.DepartmentName=Department.DepartmentName;
           DepartmentInstance.Manager=Department.Manager;
           DepartmentInstance.Phone=Department.Phone;
           DepartmentInstance.Remark=Department.Remark;


        }

        List<DepartmentRepository> Select()
        {
            DataSet ds = new DataSet();
            List<DepartmentRepository> result = new List<DepartmentRepository>();
            string str = "select * from Department where DepartmentId=@DepartId";

            SqlParameter par = new SqlParameter("@DepartId", DepartmentInstance.DepartmentId);
            SqlCommand cmd = new SqlCommand(str, sqlconn);
            cmd.Parameters.Add(par);

            try
            {
                sqlconn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds, "Result");
                
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    DepartmentRepository a = new DepartmentRepository(Convert.ToInt32(row[0]), row[1].ToString().Trim(), row[2].ToString().Trim(), row[3].ToString().Trim(), row[4].ToString().Trim());
                    result.Add(a);
                }

            }
            catch (Exception e)
            {
               
                
            }

            finally
            {
                sqlconn.Close();
            }

            return result;

        }

        List<DepartmentRepository> SelectAll()
        {
             DataSet ds = new DataSet();
            List<DepartmentRepository> result = new List<DepartmentRepository>();
            string str = "select * from Department";

            SqlCommand cmd = new SqlCommand(str, sqlconn);
           

            try
            {
                sqlconn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds, "Result");
                
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    DepartmentRepository a = new DepartmentRepository(Convert.ToInt32(row[0]), row[1].ToString().Trim(), row[2].ToString().Trim(), row[3].ToString().Trim(), row[4].ToString().Trim());
                    result.Add(a);
                }

            }
            catch (Exception e)
            {
               
                
            }

            finally
            {
                sqlconn.Close();
            }

            return result;
        }
	}
}
